"""
Socket模拟http请求

requests -> urlib -> socket
"""
import socket
import time
from urllib.parse import urlparse


def get_url(url):
    # 通过Socket请求html
    url = urlparse(url)
    host = url.netloc
    path = url.path
    if not path:
        path = "/"

    # 建立Socket连接
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((host, 80))

    client.send("GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(path, host).encode("utf8"))

    data = b""
    while True:
        d = client.recv(1024)
        if d:
            data += d
        else:
            break

    data = data.decode("utf8")
    # 通过两个回车换行符去掉头部信息
    html_data = data.split("\r\n\r\n")[1]
    print(html_data)

    client.close()


def use_socket_http():
    start_time = time.time()
    for url in range(1, 10):
        url = "http://shop.projectsedu.com/goods/{}/".format(url)
        get_url(url)
    print(time.time() - start_time)


if __name__ == '__main__':
    use_socket_http()
